// subPages/glucometer/record/record.js
const app = getApp()
var util =  require('../../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    array: ["请选择测量点","早餐前","早餐后","午餐前","午餐后","晚餐前","晚餐后"],
    index:0,
    downImg:'/resources/glucometer/down.png',
    upImg: '/resources/glucometer/up.png',
    bloodData: '0.0',
    symbol: '<',
    pBtnTxt:{
      pBtnTxt: '保存'
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      timePointImgsrc:this.data.downImg
    })
  },

  //picker
  bindTimePointChange: function (e) {
    this.setData({
      index: e.detail.value,
      timePoint: this.data.array[e.detail.value],
      timePointImgsrc: this.data.downImg,
    })
  },
  
  bindTimePointCancel:function(){
    this.setData({
      timePointImgsrc: this.data.downImg
    })
  },

  //选择箭头
  imgChange:function(e)
  {
    var that = this
    if(that.data.timePointImgsrc == that.data.downImg)
    {
      that.setData({
        timePointImgsrc: that.data.upImg
      })
    }
  },

  //读取按钮
  readBtn:function(){
    var that = this
    wx.request({
      url: app.globalData.apiUrl +'/blood/info',
      data: {
        userId: app.globalData.userId
      },
      method: 'get',
      header: {
        'content-type': 'application/json' //默认值
      },
      success: function (res) {
        // console.log(res)
        if(res.data.code == '200'){
          that.setData({
            bloodData: res.data.data
          })
        }else{
          util.showModal('提示','当前用户没有测量过,请测完重试')
          return
        }
      },
      fail: function (res) {
        console.log("失败",res);
      }
    })
  },

  //保存
  lickBtn:function(){
    var that = this
    if(that.data.timePoint == '' || that.data.timePoint == '请选择测量点')
    {
      util.showModal('提示','请选择测量点')
      return;
    }
    let type =  that.typeConversion(that.data.timePoint);
    if(type == 0){
      util.showModal('提示','请选择测量点')
      return;
    }
    wx.request({
      url: app.globalData.apiUrl +'/sugar',
      data: {
        userId: app.globalData.userId,
        type: type
      },
      method: 'put',
      header: {
        'content-type': 'application/json' //默认值
      },
      success: function (res) {
        // console.log(res)
        if(res.data.code == '200'){
          that.setData({
            bloodData: res.data.data
          })
        }else{
          util.showModal('提示','当前用户没有测量过,请测完重试')
          return;
        }
      },
      fail: function (res) {
        console.log("失败",res);
      }
    })
  },

  //测量时间点类型转换
  typeConversion:function(type){
    if(type == '早餐前'){
      return 1
    }
    if(type == '早餐后'){
      return 2
    }
    if(type == '午餐前'){
      return 3
    }
    if(type == '午餐后'){
      return 4
    }
    if(type == '晚餐前'){
      return 5
    }
    if(type == '晚餐后'){
      return 6
    }else{
      return 0 
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})